from Products.Archetypes.public import ImageField, ImageWidget, StringField, StringWidget, SelectionWidget, TextField, RichWidget
from Products.Archetypes.atapi import DisplayList
from Products.ATContentTypes.interface import IATFolder, IATFile
from archetypes.schemaextender.field import ExtensionField
from archetypes.schemaextender.interfaces import ISchemaExtender, IBrowserLayerAwareExtender
from apkn.templates.interfaces import ITemplatesLayer
from zope.component import adapts
from zope.interface import implements

class _ExtensionImageField(ExtensionField, ImageField): pass
class _ExtensionStringField(ExtensionField, StringField): pass
class _ExtensionTextField(ExtensionField, TextField): pass

POSITION_LIST = DisplayList((
    ('left', 'Left'), 
    ('right', 'Right'),
    ))
    
CONFIRM_LIST = DisplayList((
    ('yes', 'Yes'), 
    ('no', 'No'),
    ))

class FolderImageExtender(object):
    """
    Adapter to add Image and title to Folder items
    """
    adapts(IATFolder)
    implements(ISchemaExtender, IBrowserLayerAwareExtender)

    layer = ITemplatesLayer

    fields = [
        _ExtensionImageField(
            "folderimage",
                widget = ImageWidget(
                label=u"Folder Image",
                description=u"Image to display beside the folder header. The optimal size of an image is a maximum of 50 pixels tall and a maximum of 400 pixels wide. We DO NOT resize the images.",
            ),
        ),
        _ExtensionStringField(
            "folderimagetitle",
            widget = StringWidget(
                label=u"Image Title",
                description=u"Title of image to display with the folder",
            ),
        ),
        _ExtensionStringField(
            "inheritparentimage",
                vocabulary = CONFIRM_LIST,
                required = False,
                default = 'no',
                widget = SelectionWidget(
                label=u"Inherit image from parent folder?",
                description=u"Display the parent image on this folder",
                type='radio',
            ),
        ),
        _ExtensionStringField(
            "folderposition",
                vocabulary = POSITION_LIST,
                required = False,
                default = 'left',
                widget = SelectionWidget(
                label=u"Folder Image Position",
                description=u"Position to display the image",
                type='radio',
            ),
        ),
    ]

    def __init__(self, context):
        self.context = context

    def getFields(self):
        return self.fields
        
        
class FileExtender(object):
    """
    Adapter to add body text to a File Item.
    """
    adapts(IATFile)
    implements(ISchemaExtender, IBrowserLayerAwareExtender)

    layer = ITemplatesLayer

    fields = [
        _ExtensionTextField(
            "body",
            widget = RichWidget(
                label=u"Body text",
                description=u"The text to display",
                rows=25,
                allow_file_upload=False
            ),
        ),
    ]

    def __init__(self, context):
        self.context = context

    def getFields(self):
        return self.fields
        

